﻿using System;
using System.Collections.Generic;
using System.Text;
using Windows.Foundation;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Data;

namespace LayoutHelpers.Converters
{
    public class SizeConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, string language)
        {
            if (targetType != typeof(Size))
                throw new ArgumentOutOfRangeException("targetType");

            if (value == null)
                return new Size(0, 0);

            var text = value.ToString();
            var parts = text.Split(',');

            if (parts.Length != 2)
                throw new ArgumentOutOfRangeException("value");

            double width;
            double height;
            if (!double.TryParse(parts[0], out width))
                throw new ArgumentOutOfRangeException("value");

            if (!double.TryParse(parts[1], out height))
                throw new ArgumentOutOfRangeException("value");

            return new Size(width, height);
        }

        public object ConvertBack(object value, Type targetType, object parameter, string language)
        {
            throw new NotImplementedException();
        }
    }

    public class BooleanToVisibilityConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, string language)
        {
            bool result = false;

            if (value is bool)
                result = (bool)value;

            if (parameter != null)
                result = !result;

            return result ? Visibility.Visible : Visibility.Collapsed;
        }

        public object ConvertBack(object value, Type targetType, object parameter, string language)
        {
            throw new NotImplementedException();
        }
    }
}
